package com.common.www.service.programme;


import org.springframework.boot.base.jpa.service.BaseLogicService;
import com.common.www.bean.entity.programme.SupplyCompanyScheme;
import com.common.www.bean.entity.project.Project;
import com.common.www.repository.programme.SupplyCompanySchemeRepository;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Service
@Transactional
public class SupplyCompanySchemeService extends BaseLogicService<SupplyCompanyScheme, Long> {
    @Autowired
    private SupplyCompanySchemeRepository repository;

    @Override
    public SupplyCompanySchemeRepository getRepository() {
        return repository;
    }


    /**
     * 导入内线标底清单
     *
     * @param workbook
     * @return
     * @throws Exception
     */
    public List<SupplyCompanyScheme> importSimulate(Workbook workbook, Project project, String fileName, Integer jieDuan) throws Exception {
        Sheet sheet = null;
        if (workbook.getNumberOfSheets() > 0) {
            sheet = workbook.getSheetAt(0);
        }
        if (sheet == null) {
            throw new Exception("未找到导入数据,请检查文档");
        }
        List<SupplyCompanyScheme> list = new ArrayList<>();

        String gsName = "";
        for (int rowIndex = 2; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
//            System.out.println("第 " + rowIndex + " 行");
            String r0 = null;
            String r1 = null;
            String r2 = null;
            String r3 = null;
            String r4 = null;

            Row row = sheet.getRow(rowIndex);
            if (row == null) {
                break;
            }
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                cell.setCellType(CellType.STRING);
                String value = cell.getStringCellValue();

                if (cell.getColumnIndex() == 0)
                    r0 = value.trim();
                if (cell.getColumnIndex() == 1)
                    r1 = value.trim();
                if (cell.getColumnIndex() == 2)
                    r2 = value.trim();
                if (cell.getColumnIndex() == 3)
                    r3 = value.trim();
                if (cell.getColumnIndex() == 4)
                    r4 = value.trim();
            }

            if (rowIndex == 2) {
                gsName = (r4 != null && !r4.isEmpty()) ? r4 : null;
            } else if ("合计".equals(r0)) {
                continue;
            } else {
                SupplyCompanyScheme io = new SupplyCompanyScheme();
                io.setSort(Integer.valueOf(r0));
                io.setCategory(r1);
                io.setMinorItems(r2);
                io.setDetailsTerm(r3);
                io.setDetailsContent(r4);
                io.setFileName(fileName);
                io.setProject(project);
//                io.setSupplyCompany();
                io.setJieDuan(jieDuan);
                io.setSupplyCompanyName(gsName);
                list.add(io);
            }
        }
        return list;
    }


    /**
     * 按条件删除
     * @param projectId
     * @param supplyCompanyName
     * @param jieDuan
     */
    public void conditionDelete(Long projectId, String supplyCompanyName, Integer jieDuan) {
        HashMap hashMap = new HashMap();
        hashMap.put("project.id",projectId);
        hashMap.put("supplyCompanyName",supplyCompanyName);
        hashMap.put("jieDuan",jieDuan);
        List<SupplyCompanyScheme> companySchemes = queryToList(hashMap,null);
        delete(companySchemes);
    }


}
